{extend name="inherit/list" /}

{block name="title"}问题分类{/block}

{block name="content"}
<div class="layui-card-body">
    <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
    </div>

    <table id="table" lay-filter="table-filter"></table>
    <script type="text/html" id="statusTpl">
        {{#  if(d.status == 1){ }}
          <button class="layui-btn layui-btn-xs">显示</button>
        {{#  } else { }}
          <button class="layui-btn layui-btn-primary layui-btn-xs">隐藏</button>
        {{#  } }}
      </script>
    <script type="text/html" id="table-useradmin-webuser">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    </script>
</div>
{/block}

{block name="foot"}
<script>
    layui.config({
        base: '__layuiadmin__/' //静态资源所在路径
        , version: (new Date).getTime()
    }).extend({
        index: 'lib/index' //主入口模块
        , request: 'plugin/request'
    }).use(['index', 'table', 'request'], function () {
        var $ = layui.$
            , form = layui.form
            , request = layui.request
            , table = layui.table;

        var tableId = 'table'

        table.render({
            elem: '#' + tableId
            , url: "{:u('dataClassify')}"
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'id', width: 100, title: 'ID', sort: true }
                , { title: '名称', field: 'name', minWidth: 160, align: 'center' }
                , { title: '排序', field: 'sort', minWidth: 100, align: 'center' }
                , { title: '状态', field: 'status', minWidth: 100, align: 'center', templet: '#statusTpl' }
                , { title: '操作时间', field: 'update_time', minWidth: 165, align: 'center' }
                , { title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-webuser' }
            ]]
            , page: true
            , limit: 30
            , text: {
                none: '没有数据'
            }
        });

        //监听工具条
        table.on('tool(' + tableId + '-filter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                parent.layer.confirm('确认删除该数据吗？', {
                    title: '删除'
                }, function (index) {

                    request.ajax("{:u('deleteClassify')}", 'get', 'json', {
                        id: data.id
                    }, function (res) {
                        request.log(res)
                        parent.layer.msg(res.msg, {
                            time: 1500
                        }, () => {
                            if (res.code == 200) {
                                table.reload(tableId);
                            }
                        })
                    });

                    parent.layer.close(index);
                });
            } else if (obj.event === 'edit') {
                var tr = $(obj.tr);

                let url = request.url("{:u('readClassify')}", {
                    id: data.id
                })

                parent.layer.open({
                    type: 2
                    , title: '编辑分类'
                    , content: url
                    , maxmin: true
                    , area: ['470px', '290px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            request.ajax("{:u('updateClassify')}", 'post', 'json', {
                                id: field.id,
                                data: field,
                            }, function (res) {
                                request.log(res)
                                parent.layer.msg(res.msg, {
                                    time: 1500
                                    , shade: .3
                                }, () => {
                                    if (res.code == 200) {
                                        table.reload(tableId);
                                        parent.layer.close(index);
                                    }
                                })
                            });
                        });

                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                });
            }
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus(tableId)
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                parent.layer.confirm('确定批量删除勾选的数据吗？', {
                    title: '批量删除'
                }, function (index) {
                    request.ajax("{:u('deleteClassify')}", 'post', 'json', {
                        id: request.getIds(checkData)
                    }, function (res) {
                        request.log(res)
                        parent.layer.msg(res.msg, {
                            time: 1500
                        }, () => {
                            if (res.code == 200) {
                                table.reload(tableId);
                            }
                        })
                    });
                });
            }
            , add: function () {
                parent.layer.open({
                    type: 2
                    , title: '添加分类'
                    , content: "{:u('addClassify')}"
                    , maxmin: true
                    , area: ['470px', '290px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-front-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            request.ajax("{:u('addClassify')}", 'post', 'json', {
                                data: field
                            }, function (res) {
                                request.log(res)
                                parent.layer.msg(res.msg, {
                                    time: 1500
                                    , shade: .3
                                }, () => {
                                    if (res.code == 200) {
                                        table.reload(tableId);
                                        parent.layer.close(index);
                                    }
                                })
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
{/block}